package com.equis.estacionamientos.preloader {
	
	import com.equis.architecture.preloader.EquisPreloader;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	
	import mx.events.FlexEvent;

	public class Preloader extends com.equis.architecture.preloader.EquisPreloader {
		
		protected override function get dataPercentage():Number {
			return 60;
		}
		
		//	    [Embed(source="/assets/controls/ui_loader.swf", symbol="loader")]
		//	    public var PreloaderSymbol:Class;
		
		//	    public var _preloaderSymbol:MovieClip;
		
		protected var _preloader:Sprite;
		
		public function Preloader() {
			super();
			//	        _preloaderSymbol = new PreloaderSymbol() as MovieClip;
			//        	addChild(_preloaderSymbol);
			//    	    _preloaderSymbol.gotoAndStop(0);
		}
		
		public override function set preloader(preloader:Sprite):void {
			_preloader = preloader;
			preloader.addEventListener(ProgressEvent.PROGRESS,	onSWFDownloadProgress);
			preloader.addEventListener(Event.COMPLETE,			onSWFDownloadComplete);
			preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress);
			preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
			centerPreloader();
		}
		
		/**
		 * Center the preloader in the application window.
		 */
		protected function centerPreloader():void {
			//	        x = (stageWidth / 2) - (_preloaderSymbol.width / 2);
			//	        y = (stageHeight / 2) - (_preloaderSymbol.height / 2);
		}
		
		protected override function onFlexInitComplete(event:FlexEvent):void {
			_preloader.removeEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
			//			_preloaderSymbol.gotoAndStop(swfLoadProgress(100));
			super.onFlexInitComplete(event);
		}
		
		protected override function onDataProgress(event:ProgressEvent):void {
			var t:Number = event.bytesTotal;
			var l:Number = event.bytesLoaded;
			var p:Number = Math.round((l/t) * 100);
			//			_preloaderSymbol.gotoAndStop(dataLoadProgress(p));
		}
		
		protected override function onDataInitComplete(event:Event):void {
			_preloader.removeEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
			
			//	    	_preloaderSymbol.gotoAndStop(dataLoadProgress(100));
			destroyAll();
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		public override function initialize():void {
			//	        _preloaderSymbol.gotoAndStop(0);
		}
		
		protected function onSWFDownloadProgress(event:ProgressEvent):void {
			var t:Number = event.bytesTotal;
			var l:Number = event.bytesLoaded;
			var p:Number = Math.round((l/t) * 100);
			//	        _preloaderSymbol.gotoAndStop(swfLoadProgress(p));
		}
		
		protected function onSWFDownloadComplete(event:Event):void {
			//			_preloaderSymbol.gotoAndStop(swfLoadProgress(100));
		}
		
		protected function onFlexInitProgress(event:FlexEvent):void {
			//			_preloaderSymbol.gotoAndStop(swfLoadProgress(100));
		}
		
		protected function destroyAll():void {
			trace('Removing pre loader symbol.');
			//	        _preloaderSymbol.stop();
			
			//	        this.removeChild(_preloaderSymbol);
			
			_preloader = null;
			//	        _preloaderSymbol = null;
			//	        PreloaderSymbol = null;
		}
		
	}
}